class ListNode {
    public int val;
    public ListNode next;
    public ListNode pre;

    public ListNode(int val) {
        this.val = val;
    }

}

public class DoubleList {
    public ListNode head;
    public ListNode tail;

    public DoubleList() {
        this.head = new ListNode(-1);
    }

    /**
     * 头插法
     */
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head.next == null) {
            //第一次插
            this.head.next = node;
            //node.pre = this.head;
            this.tail = node;
        } else {
            node.next = this.head.next;
            node.next.pre = node;
            this.head.next = node;
        }
    }
/*    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(this.head.next == null) {
            this.head.next = node;
            node.pre = this.head;
            this.tail = node;
            return;
        }
        node.next = this.head.next;
        this.head.next = node;
        node.pre = this.head;
    }*/

    /**
     * 尾插
     */
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head.next == null) {
            this.head.next = node;
            this.tail = node;
        } else {
            this.tail.next = node;
            node.pre = this.tail;
            this.tail = node;
        }
    }

    /**
     * 任意位置插入,第一个数据节点为0号下标
     */
    public void addIndex(int index, int data) {
        //判断合法性
        ListNode node = new ListNode(data);
        ListNode cur = this.head.next;
        ListNode newHead = this.head.next;
        if (index < 0 || index > size()) {
            System.out.println("不合法");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        while (index > 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        node.pre = cur.pre;
        cur.pre.next = node;
        cur.pre = node;
        return;
    }

    /**
     * 获取单链表长度
     */
    public int size() {
        ListNode cur = this.head.next;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void display() {
        ListNode cur = this.head.next;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head.next;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            } else {
                cur = cur.next;
            }
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
/*    public void remove(int key) {
        ListNode cur = this.head.next;

        while(cur != null) {
            if(cur.val == key) {
                if(cur == this.head.next) {
                    this.head.next = this.head.next.next;
                    this.head.next.pre = this.head;
                } else {
                    cur.pre.next = cur.next;
                    if (cur.next != null) {
                        cur.next.pre = cur.pre;
                    } else {
                        this.tail = cur.pre;
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }
    }*/
    public void remove(int key) {
        ListNode cur = this.head.next;
        while (cur != null) {
            if (cur.val == key) {
                //当删除开头第一个元素时
                if (cur == this.head.next) {
                    this.head.next = cur.next;
                    cur.next.pre = null;
                    return;
                }else {
                    cur.pre.next = cur.next;
                    if (cur.next != null){
                        cur.next.pre = cur.pre;
                        return;
                    }
                    this.tail = cur.pre;
                }
            }
            cur = cur.next;
        }


    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = this.head.next;
        while (cur != null){
            if (cur.val == key){
                //如果key为第一个节点时
                if (cur == this.head.next){
                    this.head.next = cur.next;
                    cur.next.pre = null;
                }else {
                    cur.pre.next = cur.next;
                    if (cur.next != null){
                        cur.next.pre = cur.pre;
                    }else {
                        this.tail = cur.pre;
                    }
                }
            }
            cur = cur.next;
        }
    }

    //清除链表
    public void clear(){
        this.head.next = null;
    }
}

